package com.mao.jedis;

import org.junit.Test;
import redis.clients.jedis.BitPosParams;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;


public class redis {
    public static Jedis jedis;

    @Test
    public void initJedis() {
        jedis = new Jedis("192.168.213.129", 6379);
    }

    @Test
    public void testString() {
        jedis.append("mao", "wei an");
        String mao = jedis.get("mao");
        System.out.println(mao);

        jedis.del("mao");
        System.out.println(jedis.get("mao"));

        jedis.mset("name", "mao weian", "age", "67", "email", "19993");
        jedis.incr("age");
        System.out.println(jedis.get("name") + jedis.get("age") + " " + jedis.get("email"));
    }

    @Test
    public void testMap() {
        //junit 测试不保证执行顺序
        initJedis();
        HashMap<String, String> map = new HashMap<>();
        map.put("name", "mao");
        map.put("age", "100");
        map.put("email", "123456");

        //这里可能会已经存在user变量，所以可能报类型不正确的错误。
        jedis.del("user");
        jedis.hmset("user", map);
        List<String> strings = jedis.hmget("user", "name", "age", "email");
        System.out.println(strings);

        Set<String> user = jedis.hkeys("user");
        Iterator<String> iterator = user.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

        List<String> vals = jedis.hvals("user");
        for (String s : vals) {
            System.out.println(s);
        }

        jedis.del("user");
        System.out.println(jedis.exists("user"));
    }

    @Test
    public void testList() {
        initJedis();
        jedis.del("javaFramework");
        jedis.rpush("javaFramework", "spring");
        jedis.rpush("javaFramework", "springMVC");
        jedis.rpush("javaFramework", "mybatis");


        String lpop = jedis.lpop("javaFramework");
        System.out.println(lpop);
        System.out.println(jedis.llen("javaFramework"));
        //jedis.l
        System.out.println(jedis.lrange("javaFramework", 0, -1));
        System.out.println(jedis.llen("javaFramework"));
    }

    @Test
    public void testSet() {
        initJedis();
        jedis.sadd("user", "mao");
        jedis.sadd("user", "bach");
        jedis.sadd("user", "mozart");
        jedis.sadd("user", "beethoven");

        jedis.srem("user", "mao");
        System.out.println("所有值" + jedis.smembers("user"));
        Boolean ismember = jedis.sismember("user", "handel");
        System.out.println("handel" + "is member :" + ismember);

        String user = jedis.srandmember("user");
        System.out.println(user);

        System.out.println("scard" + jedis.scard("user"));
    }

    @Test
    public void testJedisListSort(){
        initJedis();
        jedis.lpush("number","9");
        jedis.lpush("number","8");
        jedis.lpush("number","7");

        jedis.rpush("number","4");
        jedis.rpush("number","5");

        System.out.println("排序"+jedis.sort("number"));
        System.out.println(jedis.lrange("number",2,-2));
        jedis.del("number");
    }

    @Test
    public void testZadd(){
        initJedis();
        jedis.del("stu");
        jedis.zadd("stu",6,"mozart");
        jedis.zadd("stu",5,"bach");
        jedis.zadd("stu", Double.valueOf(System.currentTimeMillis()), "beethoven");


        Set<String> stu = jedis.zrange("stu", 0, -1);
        String first = stu.iterator().next();
        System.out.println(first);
    }


    @Test
    public void testBit(){
        initJedis();
        BitPosParams bitPosParams = new BitPosParams(1,2);
        jedis.bitpos("bit",true,bitPosParams);
    }
}